:-use_module(library(socket)).

create_client(Host, Port) :-
        setup_call_catcher_cleanup(tcp_socket(Socket),
                                   tcp_connect(Socket, Host:Port),
                                   exception(_),
                                   tcp_close_socket(Socket)),
        setup_call_cleanup(tcp_open_socket(Socket, In, Out),
                           chat_to_server(In, Out),
                           close_connection(In, Out)).

close_connection(In, Out) :-
        close(In, [force(true)]),
        close(Out, [force(true)]).

chat_to_server(In, Out) :-
		write('here'),
        read(Command),
		(Command == 0, fail;
		write('Sending: '), write(Command), nl,
		write(Out, 'hi'),
		chat_to_server(In,Out)).